package icestorm;

import com.guo.util.ResourceUtil;

import java.io.*;

/**
 * ICE Box
 *
 * @author wangqingguo
 * @date 2018/4/1
 */
public class IceBox {
    /***
     * 启动Ice Box
     */
    private void runIceBox() {
        // icebox配置文件路径
        String resourceDirectoryPath = ResourceUtil.getResourceDirectoryPath("/iceStorm/config.icebox");
        // 启动iceBoxRun.bat
        String cmd = "cmd /C start  " + resourceDirectoryPath + "\\iceBoxRun.bat " + resourceDirectoryPath;
        //String cmd = "cmd /k start D:\\gitRepositor\\ice-demo\\iceStorm\\out\\production\\resources\\iceStorm\\config.icebox D:\\gitRepositor\\ice-demo\\iceStorm\\out\\production\\resources\\iceStorm";
        System.out.println(cmd);
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            //合并流
            SequenceInputStream sis = new SequenceInputStream(process.getInputStream(), process.getErrorStream());
            InputStreamReader isr = new InputStreamReader(sis, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while (null != (line = br.readLine())) {
                System.out.println(line);
            }
            process.destroy();
            br.close();
            isr.close();

            process.waitFor();
            int sign = process.exitValue();
            if (sign == 0) {
                System.out.println("执行结束");
            } else {
                System.out.println("执行失败");
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("child thread donn");
    }

    public static void main(String[] args) throws Exception {
        IceBox iceBox = new IceBox();
        iceBox.runIceBox();
    }
}